package com.example.hibernate.demo.controller;

import com.example.hibernate.demo.entity.BankTransferPayment;
import com.example.hibernate.demo.entity.CreditCardPayment;
import com.example.hibernate.demo.entity.Payment;
import com.example.hibernate.demo.service.PaymentService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/payments")
@RequiredArgsConstructor
public class PaymentController {
    
    private final PaymentService paymentService;
    
    @GetMapping
    public ResponseEntity<List<Payment>> getAllPayments() {
        return ResponseEntity.ok(paymentService.findAll());
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<Payment> getPaymentById(@PathVariable Long id) {
        return ResponseEntity.ok(paymentService.findById(id));
    }
    
    @GetMapping("/status/{status}")
    public ResponseEntity<List<Payment>> getPaymentsByStatus(@PathVariable Payment.PaymentStatus status) {
        return ResponseEntity.ok(paymentService.findByStatus(status));
    }
    
    @GetMapping("/date-range")
    public ResponseEntity<List<Payment>> getPaymentsByDateRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end) {
        return ResponseEntity.ok(paymentService.findByDateRange(start, end));
    }
    
    @GetMapping("/amount-greater-than")
    public ResponseEntity<List<Payment>> getPaymentsByAmountGreaterThan(@RequestParam BigDecimal amount) {
        return ResponseEntity.ok(paymentService.findByAmountGreaterThan(amount));
    }
    
    @GetMapping("/credit-card")
    public ResponseEntity<List<Payment>> getAllCreditCardPayments() {
        return ResponseEntity.ok(paymentService.findAllCreditCardPayments());
    }
    
    @GetMapping("/bank-transfer")
    public ResponseEntity<List<Payment>> getAllBankTransferPayments() {
        return ResponseEntity.ok(paymentService.findAllBankTransferPayments());
    }
    
    @PostMapping("/credit-card")
    public ResponseEntity<CreditCardPayment> createCreditCardPayment(@RequestBody CreditCardPayment payment) {
        return new ResponseEntity<>(paymentService.saveCreditCardPayment(payment), HttpStatus.CREATED);
    }
    
    @PostMapping("/bank-transfer")
    public ResponseEntity<BankTransferPayment> createBankTransferPayment(@RequestBody BankTransferPayment payment) {
        return new ResponseEntity<>(paymentService.saveBankTransferPayment(payment), HttpStatus.CREATED);
    }
    
    @PutMapping("/{id}/status")
    public ResponseEntity<Payment> updatePaymentStatus(
            @PathVariable Long id,
            @RequestParam Payment.PaymentStatus status) {
        return ResponseEntity.ok(paymentService.updateStatus(id, status));
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deletePayment(@PathVariable Long id) {
        paymentService.delete(id);
        return ResponseEntity.noContent().build();
    }
}